package com.baturu.vin.hyundai.service.impl;

import com.baturu.vin.hyundai.dto.HyundaiAttributeEntity;
import com.baturu.vin.hyundai.dto.MajorOptionDTO;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Slf4j
public class HyundaiModelDetailUtil {
    /**
     * 段落截断标识
     */
    public static final String SECTION_STRING = "\\\\@";
    /**
     * 句子截断标识
     */
    public static final String SENTENCE_STRING = "\\\\\\^";
    /**
     * 字段截断标识
     */
    public static final String FIELD_STRING = "\\\\:";
    /**
     * bpno下content明细数据转化为List
     * @param bpno 标识编码
     * @param content 内容信息
     * @return 转换后数据集
     * @throws Exception
     */
    public static List<String> getContentList (String bpno, String content) throws Exception {
        if (StringUtils.isBlank(content)) {
            String message = String.format("【%s】的【content】数据为空！", bpno);
            throw new Exception(message);
        }

        List<String> list = Lists.newArrayList(content.split(SECTION_STRING, -1));

        // 分析数据中数据总共为5行，前3行数据分别为【车型属性】【关键属性】【非关键属性（可能为空）】，其余为空值暂时不知道数据类型
        if (list.size() < 3
                || StringUtils.isBlank(list.get(0))
                || StringUtils.isBlank(list.get(1))) {
            String message = String.format("【%s】的【content】不符合已定义规范：%s", bpno, content);
            throw new Exception(message);
        }

        return list;
    }

    /**
     * 从content中获取车型属性，
     * @param contentList com.baturu.vin.hyundai.service.impl.HyundaiModelDetailUtil#getContentList(java.lang.String, java.lang.String)的结果
     * @return 车型属性
     * @throws Exception
     */
    public static String getModelAttribute(List<String> contentList) throws Exception {
        // 【content】节点【第一段】
        String carAttributeRemark = contentList.get(0);

        // 截掉【model】和【bpno】信息
        String splitCode = "\\:";
        int modelEndIdx = carAttributeRemark.indexOf(splitCode);
        int bpnoEndIdx = carAttributeRemark.indexOf(splitCode, modelEndIdx + 1);
        return carAttributeRemark.substring(bpnoEndIdx + splitCode.length());
    }

    /**
     * 从content中获取关键属性
     * @param contentList com.baturu.vin.hyundai.service.impl.HyundaiModelDetailUtil#getContentList(java.lang.String, java.lang.String)的结果
     * @return 关键属性及配置组合
     * @throws Exception
     */
    public static MajorOptionDTO getMajorOption (List<String> contentList) throws Exception {
        // 【content】节点【第二段】
        String[] majorArr = contentList.get(1).split(SENTENCE_STRING);

        // 除开最后一句为所有关键枚举信息
        Set<HyundaiAttributeEntity> attributes = new HashSet<>();
        int size = majorArr.length;
        for (int i = 0; i < size - 1; i++) {
            String majorRemark = majorArr[i];
            int typeStartIdx = 0;
            int typeEndIdx = majorRemark.indexOf(",");
            String type = majorRemark.substring(typeStartIdx, typeEndIdx);
            for (String attr : majorRemark.substring(typeEndIdx + 1).split(FIELD_STRING)) {
                String[] arr = attr.split(",", -1);
                if (".".equals(arr[0])) {
                    continue;
                }
                HyundaiAttributeEntity attribute = HyundaiAttributeEntity.builder()
                        .type(type)
                        .code(arr[0])
                        .describe(arr[1])
                        .build();
                attributes.add(attribute);
            }
        }
        // 最后一句为所有车型配置组合，样例：【USAK3F003A】下的【_F_D_W_6_D_____,_F_S_W_6_D_____】
        Set<String> combinations = Sets.newHashSet(majorArr[size - 1].split(","));

        return MajorOptionDTO.builder().attributes(attributes).combinations(combinations).build();
    }

    /**
     * 从content中获取非关键属性
     * @param contentList com.baturu.vin.hyundai.service.impl.HyundaiModelDetailUtil#getContentList(java.lang.String, java.lang.String)的结果
     * @return 非关键属性
     * @throws Exception
     */
    public static Set<HyundaiAttributeEntity> getMinorAttribute(List<String> contentList) throws Exception {
        // 【content】节点【第三段】
        String minorRemark = contentList.get(2);
        if (StringUtils.isBlank(minorRemark)) {
            return Collections.emptySet();
        }

        String[] minorArr = minorRemark.split(SENTENCE_STRING);

        // 非关键数据样例【1\:BODY PAINT COLOR\:\:ABT\:\:SMOKY GRANITE\:】
        Set<HyundaiAttributeEntity> attributes = new HashSet<>();
        for (String minor : minorArr) {
            String[] arr = minor.split(FIELD_STRING, -1);
            HyundaiAttributeEntity attribute = HyundaiAttributeEntity.builder()
                    .type(arr[1])
                    .code(arr[3])
                    .describe(arr[5])
                    .build();
            attributes.add(attribute);
        }
        return attributes;
    }
}